﻿using BLL;
using DAL;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using Model;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;

namespace HoteCode.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class RoomNumberController : ControllerBase
    {
        public RoomNumberBLL RoomNumberBLL { get; }
        public ILogger<RoomNumberController> Logger { get; }

        public RoomNumberController(RoomNumberBLL roomNumberBLL, ILogger<RoomNumberController> logger)
        {
            RoomNumberBLL = roomNumberBLL;
            Logger = logger;
        }
        [HttpPost]
        /// <summary>
        /// 房号添加
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public IActionResult RoomNumberAdd(RoomNumberModel info)
        {
            try
            {
                int res= RoomNumberBLL.RoomNumberAdd(info);
                Logger.LogInformation($"{info.NumberName}管理员正在添加房号数据,添加成功,添加时间为{DateTime.Now}");
                return Ok(res);
            }
            catch (Exception)
            {
                Logger.LogInformation($"管理员添加房号数据,添加失败,");
                throw;
            }
        }
        [HttpGet]
        /// <summary>
        /// 显示查询分页
        /// </summary>
        /// <param name="totalCount">页容量</param>
        /// <param name="totalPage">页码</param>
        /// <param name="Pageindex">当前页</param>
        /// <param name="PageSize">页码</param>
        /// <param name="NumberName">房号</param>
        /// <param name="createtime">创建时间</param>
        /// <returns></returns>
        public IActionResult GetRoomNumberList(int Pageindex = 1, int PageSize = 2, string? NumberName = "", DateTime? createtime = null)
        {
            try
            {
                int totalCount = 0; int totalPage = 0;
               var list= RoomNumberBLL.GetRoomNumberList(out totalCount, out totalPage, Pageindex, PageSize, NumberName, createtime);
                return Ok(new
                {
                    list = list,
                    totalCount = totalCount,
                    totalPage = totalPage
                });
            }
            catch (Exception)
            {

                throw;
            }
        }
        [HttpGet]
        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="Pageindex"></param>
        /// <param name="PageSize"></param>
        /// <param name="NumberName"></param>
        /// <param name="createtime"></param>
        /// <returns></returns>
        public IActionResult Eximgport(int Pageindex = 1, int PageSize = 2, string? NumberName = "", DateTime? createtime = null)
        {
            try
            {
                Logger.LogInformation($"管理员正在导出数据,导出成功,添加时间为{DateTime.Now}");
                byte[] data = null;
                string t = new FileExtensionContentTypeProvider().Mappings[".xlsx"];
                string f = "房号管理.xlsx";
                IWorkbook b= new XSSFWorkbook();
                ISheet s= b.CreateSheet();
                IRow row1=s.CreateRow(0);
                row1.CreateCell(0).SetCellValue("房型名称");
                row1.CreateCell(1).SetCellValue("房号");
                row1.CreateCell(2).SetCellValue("排序");
                row1.CreateCell(3).SetCellValue("备注");
                int totalCount = 0; int totalPage = 0;
                var list = RoomNumberBLL.GetRoomNumberList(out totalCount, out totalPage, Pageindex, PageSize, NumberName, createtime);
                int RowIndex = 1;
                foreach (var item in list)
                {
                    IRow row = s.CreateRow(RowIndex);
                    row.CreateCell(0).SetCellValue(item.RoomTypeName);
                    row.CreateCell(1).SetCellValue(item.NumberName);
                    row.CreateCell(2).SetCellValue(item.Order);
                    row.CreateCell(3).SetCellValue(item.Desc);
                    RowIndex++;
                }
                using (MemoryStream ms=new MemoryStream())
                {
                    b.Write(ms);
                    data = ms.ToArray();
                }
                return File(data, t, f);
            }
            catch (Exception)
            {
                Logger.LogInformation($"管理员正在导出数据,导出失败");
                throw;
            }
        }
        [HttpDelete]
        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult RoomNumberDel(int id)
        {
            try
            {
                Logger.LogInformation($"管理员正在删除数据,删除成功,删除时间为{DateTime.Now}");
                var res= RoomNumberBLL.RoomNumberDel(id);
                return Ok(res);
            }
            catch (Exception)
            {
                Logger.LogInformation($"管理员正在删除数据,删除失败");
                throw;
            }
        }
        [HttpDelete]
        /// <summary>
        /// 批量逻辑删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public IActionResult RoomNumberDelAll(string ids)
        {
            try
            {
                Logger.LogInformation($"管理员正在删除数据,删除成功,删除时间为{DateTime.Now}");
                var res=RoomNumberBLL.RoomNumberDelAll(ids);
                return Ok(res);
            }
            catch (Exception)
            {
                Logger.LogInformation($"管理员正在删除数据,删除失败");

                throw;
            }
        }
        [HttpPut]
        /// <summary>
        /// 批量修改状态
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public IActionResult RoomNumberUptAll(string ids, int state)
        {
            try
            {
                Logger.LogInformation($"管理员正在修改数据状态,修改成功,修改时间为{DateTime.Now}");
                var res= RoomNumberBLL.RoomNumberUptAll(ids, state);
                return Ok(res);
            }
            catch (Exception)
            {
                Logger.LogInformation($"管理员正在修改数据,修改失败");

                throw;
            }
        }
        [HttpGet]
        /// <summary>
        /// Id查询(反填)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult GetRoomNumberId(int id)
        {
            try
            {

                var list= RoomNumberBLL.GetRoomNumberId(id);
                return Ok(list);
            }
            catch (Exception)
            {

                throw;
            }
        }
        [HttpPut]
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public IActionResult GetRoomNumberNumUpdate(RoomNumberModel info)
        {
            try
            {
                Logger.LogInformation($"管理员正在修改数据,修改成功,修改时间为{DateTime.Now}");
                var list = RoomNumberBLL.GetRoomNumberNumUpdate(info);
                return Ok(list);
            }
            catch (Exception)
            {
                Logger.LogInformation($"管理员正在小学高数据,修改失败");

                throw;
            }
        }
    }
}
